

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.storages._rdb.storage &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.storages._rdb.storage</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.storages._rdb.storage 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Set</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">import</span> <span class="nn">weakref</span>

<span class="kn">import</span> <span class="nn">alembic.command</span>
<span class="kn">import</span> <span class="nn">alembic.config</span>
<span class="kn">import</span> <span class="nn">alembic.migration</span>
<span class="kn">import</span> <span class="nn">alembic.script</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">create_engine</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">Engine</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.exc</span> <span class="kn">import</span> <span class="n">IntegrityError</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.exc</span> <span class="kn">import</span> <span class="n">SQLAlchemyError</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">orm</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.sql</span> <span class="kn">import</span> <span class="n">functions</span>

<span class="kn">import</span> <span class="nn">optuna</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>
<span class="kn">from</span> <span class="nn">optuna.storages._base</span> <span class="kn">import</span> <span class="n">BaseStorage</span>
<span class="kn">from</span> <span class="nn">optuna.storages._base</span> <span class="kn">import</span> <span class="n">DEFAULT_STUDY_NAME_PREFIX</span>
<span class="kn">from</span> <span class="nn">optuna.storages._rdb</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudySummary</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">TrialState</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">version</span>


<span class="n">_logger</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="RDBStorage"><a class="viewcode-back" href="../../../../reference/storages.html#optuna.storages.RDBStorage">[文档]</a><span class="k">class</span> <span class="nc">RDBStorage</span><span class="p">(</span><span class="n">BaseStorage</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Storage class for RDB backend.</span>

<span class="sd">    Note that library users can instantiate this class, but the attributes</span>
<span class="sd">    provided by this class are not supposed to be directly accessed by them.</span>

<span class="sd">    Example:</span>

<span class="sd">        Create an :class:`~optuna.storages.RDBStorage` instance with customized</span>
<span class="sd">        ``pool_size`` and ``timeout`` settings.</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import optuna</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                x = trial.suggest_uniform(&#39;x&#39;, -100, 100)</span>
<span class="sd">                return x ** 2</span>

<span class="sd">            storage = optuna.storages.RDBStorage(</span>
<span class="sd">                url=&#39;sqlite:///:memory:&#39;,</span>
<span class="sd">                engine_kwargs={</span>
<span class="sd">                    &#39;pool_size&#39;: 20,</span>
<span class="sd">                    &#39;connect_args&#39;: {</span>
<span class="sd">                        &#39;timeout&#39;: 10</span>
<span class="sd">                    }</span>
<span class="sd">                }</span>
<span class="sd">            )</span>

<span class="sd">            study = optuna.create_study(storage=storage)</span>
<span class="sd">            study.optimize(objective, n_trials=10)</span>

<span class="sd">    Args:</span>
<span class="sd">        url: URL of the storage.</span>
<span class="sd">        engine_kwargs:</span>
<span class="sd">            A dictionary of keyword arguments that is passed to</span>
<span class="sd">            `sqlalchemy.engine.create_engine`_ function.</span>
<span class="sd">        skip_compatibility_check:</span>
<span class="sd">            Flag to skip schema compatibility check if set to True.</span>

<span class="sd">    .. _sqlalchemy.engine.create_engine:</span>
<span class="sd">        https://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine</span>

<span class="sd">    .. note::</span>
<span class="sd">        If you use MySQL, `pool_pre_ping`_ will be set to :obj:`True` by default to prevent</span>
<span class="sd">        connection timeout. You can turn it off with ``engine_kwargs[&#39;pool_pre_ping&#39;]=False``, but</span>
<span class="sd">        it is recommended to keep the setting if execution time of your objective function is</span>
<span class="sd">        longer than the `wait_timeout` of your MySQL configuration.</span>

<span class="sd">    .. _pool_pre_ping:</span>
<span class="sd">        https://docs.sqlalchemy.org/en/13/core/engines.html#sqlalchemy.create_engine.params.</span>
<span class="sd">        pool_pre_ping</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="n">engine_kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">skip_compatibility_check</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="c1"># type: (str, Optional[Dict[str, Any]], bool) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">engine_kwargs</span> <span class="o">=</span> <span class="n">engine_kwargs</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fill_storage_url_template</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">skip_compatibility_check</span> <span class="o">=</span> <span class="n">skip_compatibility_check</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_default_engine_kwargs_for_mysql</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">engine_kwargs</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">engine_kwargs</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ImportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span>
                <span class="s2">&quot;Failed to import DB access module for the specified storage URL. &quot;</span>
                <span class="s2">&quot;Please install appropriate one. (The actual import error is: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.)&quot;</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span> <span class="o">=</span> <span class="n">orm</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">(</span><span class="n">orm</span><span class="o">.</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">))</span>
        <span class="n">models</span><span class="o">.</span><span class="n">BaseModel</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span> <span class="o">=</span> <span class="n">_VersionManager</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">skip_compatibility_check</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">check_table_schema_compatibility</span><span class="p">()</span>

        <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_finalize</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[Any, Any]</span>

        <span class="n">state</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">del</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;scoped_session&quot;</span><span class="p">]</span>
        <span class="k">del</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;engine&quot;</span><span class="p">]</span>
        <span class="k">del</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;_version_manager&quot;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">state</span>

    <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="c1"># type: (Dict[Any, Any]) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">engine_kwargs</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ImportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span>
                <span class="s2">&quot;Failed to import DB access module for the specified storage URL. &quot;</span>
                <span class="s2">&quot;Please install appropriate one. (The actual import error is: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.)&quot;</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span> <span class="o">=</span> <span class="n">orm</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">(</span><span class="n">orm</span><span class="o">.</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">))</span>
        <span class="n">models</span><span class="o">.</span><span class="n">BaseModel</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span> <span class="o">=</span> <span class="n">_VersionManager</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_compatibility_check</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">check_table_schema_compatibility</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">create_new_study</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (Optional[str]) -&gt; int</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">study_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">study_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_study_name</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="p">(</span><span class="n">study_name</span><span class="o">=</span><span class="n">study_name</span><span class="p">,</span> <span class="n">direction</span><span class="o">=</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">NOT_SET</span><span class="p">)</span>
        <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">study</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">optuna</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">DuplicatedStudyError</span><span class="p">(</span>
                <span class="s2">&quot;Another study with name &#39;</span><span class="si">{}</span><span class="s2">&#39; already exists. &quot;</span>
                <span class="s2">&quot;Please specify a different name, or reuse the existing one &quot;</span>
                <span class="s2">&quot;by setting `load_if_exists` (for Python API) or &quot;</span>
                <span class="s2">&quot;`--skip-if-exists` flag (for CLI).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_name</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;A new study created with name: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">study_name</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">study</span><span class="o">.</span><span class="n">study_id</span>

    <span class="k">def</span> <span class="nf">delete_study</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">session</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">study</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_create_unique_study_name</span><span class="p">(</span><span class="n">session</span><span class="p">):</span>
        <span class="c1"># type: (orm.Session) -&gt; str</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">study_uuid</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">())</span>
            <span class="n">study_name</span> <span class="o">=</span> <span class="n">DEFAULT_STUDY_NAME_PREFIX</span> <span class="o">+</span> <span class="n">study_uuid</span>
            <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_by_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">study</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">study_name</span>

    <span class="c1"># TODO(sano): Prevent simultaneously setting different direction in distributed environments.</span>
    <span class="k">def</span> <span class="nf">set_study_direction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">direction</span><span class="p">):</span>
        <span class="c1"># type: (int, StudyDirection) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">!=</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">NOT_SET</span> <span class="ow">and</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">!=</span> <span class="n">direction</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Cannot overwrite study direction from </span><span class="si">{}</span><span class="s2"> to </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">study</span><span class="o">.</span><span class="n">direction</span><span class="p">,</span> <span class="n">direction</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">=</span> <span class="n">direction</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_study_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyUserAttributeModel</span><span class="o">.</span><span class="n">find_by_study_and_key</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyUserAttributeModel</span><span class="p">(</span>
                <span class="n">study_id</span><span class="o">=</span><span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attribute</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_study_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudySystemAttributeModel</span><span class="o">.</span><span class="n">find_by_study_and_key</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudySystemAttributeModel</span><span class="p">(</span>
                <span class="n">study_id</span><span class="o">=</span><span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attribute</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_id_from_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_name</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; int</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">study</span><span class="o">.</span><span class="n">study_id</span>

    <span class="k">def</span> <span class="nf">get_study_id_from_trial_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; int</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trial</span><span class="o">.</span><span class="n">study_id</span>

    <span class="k">def</span> <span class="nf">get_study_name_from_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">study</span><span class="o">.</span><span class="n">study_name</span>

    <span class="k">def</span> <span class="nf">get_study_direction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; StudyDirection</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span>

    <span class="k">def</span> <span class="nf">get_study_user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure that that study exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">attributes</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyUserAttributeModel</span><span class="o">.</span><span class="n">where_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">user_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attributes</span><span class="p">}</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">user_attrs</span>

    <span class="k">def</span> <span class="nf">get_study_system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure that that study exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">attributes</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudySystemAttributeModel</span><span class="o">.</span><span class="n">where_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">system_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attributes</span><span class="p">}</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">system_attrs</span>

    <span class="k">def</span> <span class="nf">get_trial_user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure trial exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="n">attributes</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="o">.</span><span class="n">where_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">user_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attributes</span><span class="p">}</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">user_attrs</span>

    <span class="k">def</span> <span class="nf">get_trial_system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure trial exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="n">attributes</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="o">.</span><span class="n">where_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">system_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attributes</span><span class="p">}</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">system_attrs</span>

    <span class="k">def</span> <span class="nf">get_all_study_summaries</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">StudySummary</span><span class="p">]:</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">summarized_trial</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">study_id</span><span class="p">,</span>
                <span class="n">functions</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">)</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s2">&quot;datetime_start&quot;</span><span class="p">),</span>
                <span class="n">functions</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s2">&quot;n_trial&quot;</span><span class="p">),</span>
            <span class="p">)</span>
            <span class="o">.</span><span class="n">group_by</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">study_id</span><span class="p">)</span>
            <span class="o">.</span><span class="n">with_labels</span><span class="p">()</span>
            <span class="o">.</span><span class="n">subquery</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="n">study_summary_stmt</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span>
            <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">study_id</span><span class="p">,</span>
            <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">study_name</span><span class="p">,</span>
            <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">direction</span><span class="p">,</span>
            <span class="n">summarized_trial</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
            <span class="n">functions</span><span class="o">.</span><span class="n">coalesce</span><span class="p">(</span><span class="n">summarized_trial</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">n_trial</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s2">&quot;n_trial&quot;</span><span class="p">),</span>
        <span class="p">)</span><span class="o">.</span><span class="n">select_from</span><span class="p">(</span><span class="n">orm</span><span class="o">.</span><span class="n">outerjoin</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="p">,</span> <span class="n">summarized_trial</span><span class="p">))</span>

        <span class="n">study_summary</span> <span class="o">=</span> <span class="n">study_summary_stmt</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="n">study_summaries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">study</span> <span class="ow">in</span> <span class="n">study_summary</span><span class="p">:</span>
            <span class="n">best_trial</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[models.TrialModel]</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
                    <span class="n">best_trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_max_value_trial</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">best_trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_min_value_trial</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="n">best_trial_frozen</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[FrozenTrial]</span>
            <span class="k">if</span> <span class="n">best_trial</span><span class="p">:</span>
                <span class="n">params</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span>
                        <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">param_name</span><span class="p">,</span>
                        <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">param_value</span><span class="p">,</span>
                        <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">distribution_json</span><span class="p">,</span>
                    <span class="p">)</span>
                    <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">best_trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">)</span>
                    <span class="o">.</span><span class="n">all</span><span class="p">()</span>
                <span class="p">)</span>
                <span class="n">param_dict</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="n">param_distributions</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
                    <span class="n">distribution</span> <span class="o">=</span> <span class="n">distributions</span><span class="o">.</span><span class="n">json_to_distribution</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">distribution_json</span><span class="p">)</span>
                    <span class="n">param_dict</span><span class="p">[</span><span class="n">param</span><span class="o">.</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_external_repr</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">param_value</span><span class="p">)</span>
                    <span class="n">param_distributions</span><span class="p">[</span><span class="n">param</span><span class="o">.</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span>
                <span class="n">user_attrs</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                    <span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">best_trial</span><span class="o">.</span><span class="n">trial_id</span>
                <span class="p">)</span>
                <span class="n">system_attrs</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                    <span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">best_trial</span><span class="o">.</span><span class="n">trial_id</span>
                <span class="p">)</span>
                <span class="n">intermediate</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                    <span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">best_trial</span><span class="o">.</span><span class="n">trial_id</span>
                <span class="p">)</span>
                <span class="n">best_trial_frozen</span> <span class="o">=</span> <span class="n">FrozenTrial</span><span class="p">(</span>
                    <span class="n">best_trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span>
                    <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">,</span>
                    <span class="n">best_trial</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">best_trial</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
                    <span class="n">best_trial</span><span class="o">.</span><span class="n">datetime_complete</span><span class="p">,</span>
                    <span class="n">param_dict</span><span class="p">,</span>
                    <span class="n">param_distributions</span><span class="p">,</span>
                    <span class="p">{</span><span class="n">i</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">user_attrs</span><span class="p">},</span>
                    <span class="p">{</span><span class="n">i</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="p">},</span>
                    <span class="p">{</span><span class="n">value</span><span class="o">.</span><span class="n">step</span><span class="p">:</span> <span class="n">value</span><span class="o">.</span><span class="n">value</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">intermediate</span><span class="p">},</span>
                    <span class="n">best_trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="n">user_attrs</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">StudyUserAttributeModel</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">StudyUserAttributeModel</span><span class="o">.</span><span class="n">study_id</span> <span class="o">==</span> <span class="n">study</span><span class="o">.</span><span class="n">study_id</span>
            <span class="p">)</span>
            <span class="n">system_attrs</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">StudySystemAttributeModel</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">StudySystemAttributeModel</span><span class="o">.</span><span class="n">study_id</span> <span class="o">==</span> <span class="n">study</span><span class="o">.</span><span class="n">study_id</span>
            <span class="p">)</span>
            <span class="n">study_summaries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">StudySummary</span><span class="p">(</span>
                    <span class="n">study_name</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">study_name</span><span class="p">,</span>
                    <span class="n">direction</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">direction</span><span class="p">,</span>
                    <span class="n">best_trial</span><span class="o">=</span><span class="n">best_trial_frozen</span><span class="p">,</span>
                    <span class="n">user_attrs</span><span class="o">=</span><span class="p">{</span><span class="n">i</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">user_attrs</span><span class="p">},</span>
                    <span class="n">system_attrs</span><span class="o">=</span><span class="p">{</span><span class="n">i</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="p">},</span>
                    <span class="n">n_trials</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">n_trial</span><span class="p">,</span>
                    <span class="n">datetime_start</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
                    <span class="n">study_id</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">study_id</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">study_summaries</span>

    <span class="k">def</span> <span class="nf">create_new_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">template_trial</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (int, Optional[FrozenTrial]) -&gt; int</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_new_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">template_trial</span><span class="p">)</span><span class="o">.</span><span class="n">_trial_id</span>

    <span class="k">def</span> <span class="nf">_create_new_trial</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">template_trial</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">FrozenTrial</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">FrozenTrial</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Create a new trial and returns its trial_id and a :class:`~optuna.trial.FrozenTrial`.</span>

<span class="sd">        Args:</span>
<span class="sd">            study_id:</span>
<span class="sd">                Study id.</span>
<span class="sd">            template_trial:</span>
<span class="sd">                A :class:`~optuna.trial.FrozenTrial` with default values for trial attributes.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A :class:`~optuna.trial.FrozenTrial` instance.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure that that study exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">template_trial</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">(</span><span class="n">study_id</span><span class="o">=</span><span class="n">study_id</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Because only `RUNNING` trials can be updated,</span>
            <span class="c1"># we temporarily set the state of the new trial to `RUNNING`.</span>
            <span class="c1"># After all fields of the trial have been updated,</span>
            <span class="c1"># the state is set to `template_trial.state`.</span>
            <span class="n">temp_state</span> <span class="o">=</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span>

            <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">(</span>
                <span class="n">study_id</span><span class="o">=</span><span class="n">study_id</span><span class="p">,</span>
                <span class="n">number</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">state</span><span class="o">=</span><span class="n">temp_state</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="n">template_trial</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">datetime_start</span><span class="o">=</span><span class="n">template_trial</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
                <span class="n">datetime_complete</span><span class="o">=</span><span class="n">template_trial</span><span class="o">.</span><span class="n">datetime_complete</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="c1"># Flush the session cache to reflect the above addition operation to</span>
        <span class="c1"># the current RDB transaction.</span>
        <span class="c1">#</span>
        <span class="c1"># Without flushing, the following operations (e.g, `_set_trial_param_without_commit`)</span>
        <span class="c1"># will fail because the target trial doesn&#39;t exist in the storage yet.</span>
        <span class="n">session</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">template_trial</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value</span> <span class="ow">in</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">distribution</span> <span class="o">=</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
                <span class="n">param_value_in_internal_repr</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_param_without_commit</span><span class="p">(</span>
                    <span class="n">session</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">,</span> <span class="n">distribution</span>
                <span class="p">)</span>

            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">user_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_user_attr_without_commit</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">system_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_system_attr_without_commit</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span> <span class="ow">in</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">intermediate_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_intermediate_value_without_commit</span><span class="p">(</span>
                    <span class="n">session</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span>
                <span class="p">)</span>

            <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">template_trial</span><span class="o">.</span><span class="n">state</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">number</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">count_past_trials</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
        <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">template_trial</span><span class="p">:</span>
            <span class="n">frozen</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">template_trial</span><span class="p">)</span>
            <span class="n">frozen</span><span class="o">.</span><span class="n">number</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">number</span>
            <span class="n">frozen</span><span class="o">.</span><span class="n">datetime_start</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">datetime_start</span>
            <span class="n">frozen</span><span class="o">.</span><span class="n">_trial_id</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">frozen</span> <span class="o">=</span> <span class="n">FrozenTrial</span><span class="p">(</span>
                <span class="n">number</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span>
                <span class="n">state</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">datetime_start</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
                <span class="n">datetime_complete</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">distributions</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">user_attrs</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">system_attrs</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">intermediate_values</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">trial_id</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">frozen</span>

    <span class="k">def</span> <span class="nf">_update_trial</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">trial_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
        <span class="n">state</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">TrialState</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">value</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">intermediate_values</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">params</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">distributions_</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">BaseDistribution</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">user_attrs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">system_attrs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">datetime_complete</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Sync latest trial updates to a database.</span>

<span class="sd">        Args:</span>
<span class="sd">            trial_id:</span>
<span class="sd">                Trial id of the trial to update.</span>
<span class="sd">            state:</span>
<span class="sd">                New state. None when there are no changes.</span>
<span class="sd">            value:</span>
<span class="sd">                New value. None when there are no changes.</span>
<span class="sd">            intermediate_values:</span>
<span class="sd">                New intermediate values. None when there are no updates.</span>
<span class="sd">            params:</span>
<span class="sd">                New parameter dictionary. None when there are no updates.</span>
<span class="sd">            distributions_:</span>
<span class="sd">                New parameter distributions. None when there are no updates.</span>
<span class="sd">            user_attrs:</span>
<span class="sd">                New user_attr. None when there are no updates.</span>
<span class="sd">            system_attrs:</span>
<span class="sd">                New system_attr. None when there are no updates.</span>
<span class="sd">            datetime_complete:</span>
<span class="sd">                Completion time of the trial. Set if and only if this method</span>
<span class="sd">                change the state of trial into one of the finished states.</span>

<span class="sd">        Returns:</span>
<span class="sd">            True when success.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>
        <span class="n">trial_model</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">)</span>
            <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
            <span class="o">.</span><span class="n">with_for_update</span><span class="p">()</span>
            <span class="o">.</span><span class="n">one_or_none</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">trial_model</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">NOT_FOUND_MSG</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trial_model</span><span class="o">.</span><span class="n">state</span><span class="o">.</span><span class="n">is_finished</span><span class="p">():</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Cannot change attributes of finished trial.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="n">state</span>
            <span class="ow">and</span> <span class="n">trial_model</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">state</span>
            <span class="ow">and</span> <span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span>
            <span class="ow">and</span> <span class="n">trial_model</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">WAITING</span>
        <span class="p">):</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">state</span><span class="p">:</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">state</span>

        <span class="k">if</span> <span class="n">datetime_complete</span><span class="p">:</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">datetime_complete</span> <span class="o">=</span> <span class="n">datetime_complete</span>

        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">if</span> <span class="n">user_attrs</span><span class="p">:</span>
            <span class="n">trial_user_attrs</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="p">)</span>
                <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
                <span class="o">.</span><span class="n">with_for_update</span><span class="p">()</span>
                <span class="o">.</span><span class="n">all</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">trial_user_attrs_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">attr</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">trial_user_attrs</span><span class="p">}</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">user_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">trial_user_attrs_dict</span><span class="p">:</span>
                    <span class="n">trial_user_attrs_dict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial_user_attrs_dict</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">user_attributes</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">k</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>
                <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">user_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">trial_user_attrs_dict</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">system_attrs</span><span class="p">:</span>
            <span class="n">trial_system_attrs</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="p">)</span>
                <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
                <span class="o">.</span><span class="n">with_for_update</span><span class="p">()</span>
                <span class="o">.</span><span class="n">all</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">trial_system_attrs_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">attr</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">trial_system_attrs</span><span class="p">}</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">trial_system_attrs_dict</span><span class="p">:</span>
                    <span class="n">trial_system_attrs_dict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial_system_attrs_dict</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">system_attributes</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">k</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>
                <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">trial_system_attrs_dict</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">intermediate_values</span><span class="p">:</span>
            <span class="n">value_models</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="p">)</span>
                <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
                <span class="o">.</span><span class="n">with_for_update</span><span class="p">()</span>
                <span class="o">.</span><span class="n">all</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">value_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">value_model</span><span class="o">.</span><span class="n">step</span><span class="p">:</span> <span class="n">value_model</span> <span class="k">for</span> <span class="n">value_model</span> <span class="ow">in</span> <span class="n">value_models</span><span class="p">}</span>
            <span class="k">for</span> <span class="n">s</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">value_dict</span><span class="p">:</span>
                    <span class="n">value_dict</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
                    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">value_dict</span><span class="p">[</span><span class="n">s</span><span class="p">])</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="p">(</span><span class="n">step</span><span class="o">=</span><span class="n">s</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">v</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">s</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">intermediate_values</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">s</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value_dict</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">params</span> <span class="ow">and</span> <span class="n">distributions_</span><span class="p">:</span>
            <span class="n">trial_param</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="p">)</span>
                <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
                <span class="o">.</span><span class="n">all</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">param_keys</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">param_name</span> <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">trial_param</span><span class="p">)</span>
            <span class="n">trial_model</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="p">(</span>
                    <span class="n">param_name</span><span class="o">=</span><span class="n">param_name</span><span class="p">,</span>
                    <span class="n">param_value</span><span class="o">=</span><span class="n">param_value</span><span class="p">,</span>
                    <span class="n">distribution_json</span><span class="o">=</span><span class="n">distributions</span><span class="o">.</span><span class="n">distribution_to_json</span><span class="p">(</span>
                        <span class="n">distributions_</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
                    <span class="p">),</span>
                <span class="p">)</span>
                <span class="k">for</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">param_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">param_keys</span>
            <span class="p">)</span>
        <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial_model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">set_trial_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="c1"># type: (int, TrialState) -&gt; bool</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">for_update</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trial</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">NOT_FOUND_MSG</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span> <span class="ow">and</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">WAITING</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">state</span>
        <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">is_finished</span><span class="p">():</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">datetime_complete</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value_internal</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (int, str, float, distributions.BaseDistribution) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_param_without_commit</span><span class="p">(</span>
            <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value_internal</span><span class="p">,</span> <span class="n">distribution</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_trial_param_without_commit</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value_internal</span><span class="p">,</span> <span class="n">distribution</span>
    <span class="p">):</span>
        <span class="c1"># type: (orm.Session, int, str, float, distributions.BaseDistribution) -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">trial_param</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">find_by_trial_and_param_name</span><span class="p">(</span>
            <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">session</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="n">trial_param</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Raise error in case distribution is incompatible.</span>
            <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">json_to_distribution</span><span class="p">(</span><span class="n">trial_param</span><span class="o">.</span><span class="n">distribution_json</span><span class="p">),</span> <span class="n">distribution</span>
            <span class="p">)</span>

            <span class="n">trial_param</span><span class="o">.</span><span class="n">param_value</span> <span class="o">=</span> <span class="n">param_value_internal</span>
            <span class="n">trial_param</span><span class="o">.</span><span class="n">distribution_json</span> <span class="o">=</span> <span class="n">distributions</span><span class="o">.</span><span class="n">distribution_to_json</span><span class="p">(</span><span class="n">distribution</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">trial_param</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="p">(</span>
                <span class="n">trial_id</span><span class="o">=</span><span class="n">trial_id</span><span class="p">,</span>
                <span class="n">param_name</span><span class="o">=</span><span class="n">param_name</span><span class="p">,</span>
                <span class="n">param_value</span><span class="o">=</span><span class="n">param_value_internal</span><span class="p">,</span>
                <span class="n">distribution_json</span><span class="o">=</span><span class="n">distributions</span><span class="o">.</span><span class="n">distribution_to_json</span><span class="p">(</span><span class="n">distribution</span><span class="p">),</span>
            <span class="p">)</span>

            <span class="n">trial_param</span><span class="o">.</span><span class="n">check_and_add</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_or_set_param_distribution</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">trial_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
        <span class="n">param_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">param_value_internal</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">distribution</span><span class="p">:</span> <span class="n">distributions</span><span class="o">.</span><span class="n">BaseDistribution</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Acquire a lock of this trial.</span>
        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">for_update</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trial</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">NOT_FOUND_MSG</span><span class="p">)</span>

        <span class="n">previous_record</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="p">)</span>
            <span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">)</span>
            <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">study_id</span> <span class="o">==</span> <span class="n">trial</span><span class="o">.</span><span class="n">study_id</span><span class="p">)</span>
            <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">param_name</span> <span class="o">==</span> <span class="n">param_name</span><span class="p">)</span>
            <span class="o">.</span><span class="n">first</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">previous_record</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">json_to_distribution</span><span class="p">(</span><span class="n">previous_record</span><span class="o">.</span><span class="n">distribution_json</span><span class="p">),</span>
                <span class="n">distribution</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="p">(</span>
                    <span class="n">trial_id</span><span class="o">=</span><span class="n">trial_id</span><span class="p">,</span>
                    <span class="n">param_name</span><span class="o">=</span><span class="n">param_name</span><span class="p">,</span>
                    <span class="n">param_value</span><span class="o">=</span><span class="n">param_value_internal</span><span class="p">,</span>
                    <span class="n">distribution_json</span><span class="o">=</span><span class="n">distributions</span><span class="o">.</span><span class="n">distribution_to_json</span><span class="p">(</span><span class="n">distribution</span><span class="p">),</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="c1"># Release lock.</span>
        <span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_trial_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">):</span>
        <span class="c1"># type: (int, str) -&gt; float</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">trial_param</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialParamModel</span><span class="o">.</span><span class="n">find_or_raise_by_trial_and_param_name</span><span class="p">(</span>
            <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">session</span>
        <span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trial_param</span><span class="o">.</span><span class="n">param_value</span>

    <span class="k">def</span> <span class="nf">set_trial_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, float) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_intermediate_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span><span class="p">):</span>
        <span class="c1"># type: (int, int, float) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_intermediate_value_without_commit</span><span class="p">(</span>
            <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_trial_intermediate_value_without_commit</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span>
    <span class="p">):</span>
        <span class="c1"># type: (orm.Session, int, int, float) -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">trial_value</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="o">.</span><span class="n">find_by_trial_and_step</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trial_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">trial_value</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialValueModel</span><span class="p">(</span>
                <span class="n">trial_id</span><span class="o">=</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">intermediate_value</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">trial_value</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">trial_value</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">intermediate_value</span>

    <span class="k">def</span> <span class="nf">set_trial_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_user_attr_without_commit</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_trial_user_attr_without_commit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (orm.Session, int, str, Any) -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="o">.</span><span class="n">find_by_trial_and_key</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialUserAttributeModel</span><span class="p">(</span>
                <span class="n">trial_id</span><span class="o">=</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attribute</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial_system_attr_without_commit</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_trial_system_attr_without_commit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">session</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (orm.Session, int, str, Any) -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="o">.</span><span class="n">find_by_trial_and_key</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">attribute</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attribute</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialSystemAttributeModel</span><span class="p">(</span>
                <span class="n">trial_id</span><span class="o">=</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value_json</span><span class="o">=</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attribute</span><span class="o">.</span><span class="n">value_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_trial_number_from_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; int</span>

        <span class="n">trial_number</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">number</span>
        <span class="k">return</span> <span class="n">trial_number</span>

    <span class="k">def</span> <span class="nf">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; FrozenTrial</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">trial_model</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">)</span>
            <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">trial_id</span> <span class="o">==</span> <span class="n">trial_id</span><span class="p">)</span>
            <span class="o">.</span><span class="n">one_or_none</span><span class="p">()</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">trial_model</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;No trial with trial-id </span><span class="si">{}</span><span class="s2"> found.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>

        <span class="n">frozen_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_frozen_trial_from_trial_model</span><span class="p">(</span><span class="n">trial_model</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">frozen_trial</span>

    <span class="k">def</span> <span class="nf">get_all_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">deepcopy</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># type: (int, bool) -&gt; List[FrozenTrial]</span>

        <span class="n">trials</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="nb">set</span><span class="p">())</span>

        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">trials</span><span class="p">)</span> <span class="k">if</span> <span class="n">deepcopy</span> <span class="k">else</span> <span class="n">trials</span>

    <span class="k">def</span> <span class="nf">_get_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">excluded_trial_ids</span><span class="p">:</span> <span class="n">Set</span><span class="p">[</span><span class="nb">int</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">FrozenTrial</span><span class="p">]:</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># Ensure that the study exists.</span>
        <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="n">trial_models</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">)</span>
            <span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                <span class="o">~</span><span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">trial_id</span><span class="o">.</span><span class="n">in_</span><span class="p">(</span><span class="n">excluded_trial_ids</span><span class="p">),</span>
                <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">study_id</span> <span class="o">==</span> <span class="n">study_id</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="n">trials</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_build_frozen_trial_from_trial_model</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span> <span class="k">for</span> <span class="n">trial</span> <span class="ow">in</span> <span class="n">trial_models</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trials</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_build_frozen_trial_from_trial_model</span><span class="p">(</span><span class="n">trial</span><span class="p">:</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">FrozenTrial</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">FrozenTrial</span><span class="p">(</span>
            <span class="n">number</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span>
            <span class="n">state</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">,</span>
            <span class="n">value</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">datetime_start</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">datetime_start</span><span class="p">,</span>
            <span class="n">datetime_complete</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">datetime_complete</span><span class="p">,</span>
            <span class="n">params</span><span class="o">=</span><span class="p">{</span>
                <span class="n">p</span><span class="o">.</span><span class="n">param_name</span><span class="p">:</span> <span class="n">distributions</span><span class="o">.</span><span class="n">json_to_distribution</span><span class="p">(</span>
                    <span class="n">p</span><span class="o">.</span><span class="n">distribution_json</span>
                <span class="p">)</span><span class="o">.</span><span class="n">to_external_repr</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">param_value</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span>
            <span class="p">},</span>
            <span class="n">distributions</span><span class="o">=</span><span class="p">{</span>
                <span class="n">p</span><span class="o">.</span><span class="n">param_name</span><span class="p">:</span> <span class="n">distributions</span><span class="o">.</span><span class="n">json_to_distribution</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">distribution_json</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span>
            <span class="p">},</span>
            <span class="n">user_attrs</span><span class="o">=</span><span class="p">{</span><span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">user_attributes</span><span class="p">},</span>
            <span class="n">system_attrs</span><span class="o">=</span><span class="p">{</span>
                <span class="n">attr</span><span class="o">.</span><span class="n">key</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">value_json</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">system_attributes</span>
            <span class="p">},</span>
            <span class="n">intermediate_values</span><span class="o">=</span><span class="p">{</span><span class="n">value</span><span class="o">.</span><span class="n">step</span><span class="p">:</span> <span class="n">value</span><span class="o">.</span><span class="n">value</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">values</span><span class="p">},</span>
            <span class="n">trial_id</span><span class="o">=</span><span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_best_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; FrozenTrial</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
            <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_max_value_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">trial</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">find_min_value_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>

        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_n_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (int, Optional[TrialState]) -&gt; int</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>
        <span class="n">study</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="n">n_trials</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TrialModel</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>

        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">n_trials</span>

    <span class="k">def</span> <span class="nf">read_trials_from_remote_storage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># Make sure that the given study exists.</span>
        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>
        <span class="n">models</span><span class="o">.</span><span class="n">StudyModel</span><span class="o">.</span><span class="n">find_or_raise_by_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">session</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_set_default_engine_kwargs_for_mysql</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">engine_kwargs</span><span class="p">):</span>
        <span class="c1"># type: (str, Dict[str, Any]) -&gt; None</span>

        <span class="c1"># Skip if RDB is not MySQL.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">url</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;mysql&quot;</span><span class="p">):</span>
            <span class="k">return</span>

        <span class="c1"># Do not overwrite value.</span>
        <span class="k">if</span> <span class="s2">&quot;pool_pre_ping&quot;</span> <span class="ow">in</span> <span class="n">engine_kwargs</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="c1"># If True, the connection pool checks liveness of connections at every checkout.</span>
        <span class="c1"># Without this option, trials that take longer than `wait_timeout` may cause connection</span>
        <span class="c1"># errors. For further details, please refer to the following document:</span>
        <span class="c1"># https://docs.sqlalchemy.org/en/13/core/pooling.html#pool-disconnects-pessimistic</span>
        <span class="n">engine_kwargs</span><span class="p">[</span><span class="s2">&quot;pool_pre_ping&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;pool_pre_ping=True was set to engine_kwargs to prevent connection timeout.&quot;</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_fill_storage_url_template</span><span class="p">(</span><span class="n">template</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; str</span>

        <span class="k">return</span> <span class="n">template</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">SCHEMA_VERSION</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SCHEMA_VERSION</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">):</span>
        <span class="c1"># type: (orm.Session) -&gt; bool</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">IntegrityError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s2">&quot;Ignoring </span><span class="si">{}</span><span class="s2">. This happens due to a timing issue among threads/processes/nodes. &quot;</span>
                <span class="s2">&quot;Another one might have committed a record with the same key(s).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="p">)</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">):</span>
        <span class="c1"># type: (orm.Session) -&gt; None</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">SQLAlchemyError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">session</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="n">message</span> <span class="o">=</span> <span class="p">(</span>
                <span class="s2">&quot;An exception is raised during the commit. &quot;</span>
                <span class="s2">&quot;This typically happens due to invalid data in the commit, &quot;</span>
                <span class="s2">&quot;e.g. exceeding max length. &quot;</span>
                <span class="s2">&quot;(The actual exception is as follows: </span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="p">)</span>
            <span class="k">raise</span> <span class="n">optuna</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">StorageInternalError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">remove_session</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>
        <span class="sd">&quot;&quot;&quot;Removes the current session.</span>

<span class="sd">        A session is stored in SQLAlchemy&#39;s ThreadLocalRegistry for each thread. This method</span>
<span class="sd">        closes and removes the session which is associated to the current thread. Particularly,</span>
<span class="sd">        under multi-thread use cases, it is important to call this method *from each thread*.</span>
<span class="sd">        Otherwise, all sessions and their associated DB connections are destructed by a thread</span>
<span class="sd">        that occasionally invoked the garbage collector. By default, it is not allowed to touch</span>
<span class="sd">        a SQLite connection from threads other than the thread that created the connection.</span>
<span class="sd">        Therefore, we need to explicitly close the connection from each thread.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="c1"># This destructor calls remove_session to explicitly close the DB connection. We need this</span>
        <span class="c1"># because DB connections created in SQLAlchemy are not automatically closed by reference</span>
        <span class="c1"># counters, so it is not guaranteed that they are released by correct threads (for more</span>
        <span class="c1"># information, please see the docstring of remove_session).</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s2">&quot;scoped_session&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">remove_session</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">upgrade</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>
        <span class="sd">&quot;&quot;&quot;Upgrade the storage schema.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">upgrade</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_current_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>
        <span class="sd">&quot;&quot;&quot;Return the schema version currently used by this storage.&quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">get_current_version</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_head_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>
        <span class="sd">&quot;&quot;&quot;Return the latest schema version.&quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">get_head_version</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_all_versions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; List[str]</span>
        <span class="sd">&quot;&quot;&quot;Return the schema version list.&quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_version_manager</span><span class="o">.</span><span class="n">get_all_versions</span><span class="p">()</span></div>


<span class="k">class</span> <span class="nc">_VersionManager</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">scoped_session</span><span class="p">):</span>
        <span class="c1"># type: (str, Engine, orm.scoped_session) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="n">url</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">engine</span> <span class="o">=</span> <span class="n">engine</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span> <span class="o">=</span> <span class="n">scoped_session</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_init_version_info_model</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_alembic</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_init_version_info_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">version_info</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">VersionInfoModel</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">version_info</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
            <span class="n">RDBStorage</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="n">version_info</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">VersionInfoModel</span><span class="p">(</span>
            <span class="n">schema_version</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SCHEMA_VERSION</span><span class="p">,</span> <span class="n">library_version</span><span class="o">=</span><span class="n">version</span><span class="o">.</span><span class="n">__version__</span>
        <span class="p">)</span>

        <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">version_info</span><span class="p">)</span>
        <span class="n">RDBStorage</span><span class="o">.</span><span class="n">_commit_with_integrity_check</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_init_alembic</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">&quot;alembic&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">WARN</span><span class="p">)</span>

        <span class="n">context</span> <span class="o">=</span> <span class="n">alembic</span><span class="o">.</span><span class="n">migration</span><span class="o">.</span><span class="n">MigrationContext</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">connect</span><span class="p">())</span>
        <span class="n">is_initialized</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_current_revision</span><span class="p">()</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">is_initialized</span><span class="p">:</span>
            <span class="c1"># The `alembic_version` table already exists and is not empty.</span>
            <span class="k">return</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_alembic_supported</span><span class="p">():</span>
            <span class="n">revision</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_head_version</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># The storage has been created before alembic is introduced.</span>
            <span class="n">revision</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_base_version</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_set_alembic_revision</span><span class="p">(</span><span class="n">revision</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_alembic_revision</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">revision</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; None</span>

        <span class="n">context</span> <span class="o">=</span> <span class="n">alembic</span><span class="o">.</span><span class="n">migration</span><span class="o">.</span><span class="n">MigrationContext</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">connect</span><span class="p">())</span>
        <span class="n">script</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_script</span><span class="p">()</span>
        <span class="n">context</span><span class="o">.</span><span class="n">stamp</span><span class="p">(</span><span class="n">script</span><span class="p">,</span> <span class="n">revision</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check_table_schema_compatibility</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="c1"># NOTE: After invocation of `_init_version_info_model` method,</span>
        <span class="c1">#       it is ensured that a `VersionInfoModel` entry exists.</span>
        <span class="n">version_info</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">VersionInfoModel</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="n">RDBStorage</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">assert</span> <span class="n">version_info</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

        <span class="n">current_version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_current_version</span><span class="p">()</span>
        <span class="n">head_version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_head_version</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">current_version</span> <span class="o">==</span> <span class="n">head_version</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">message</span> <span class="o">=</span> <span class="p">(</span>
            <span class="s2">&quot;The runtime optuna version </span><span class="si">{}</span><span class="s2"> is no longer compatible with the table schema &quot;</span>
            <span class="s2">&quot;(set up by optuna </span><span class="si">{}</span><span class="s2">). &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">version</span><span class="o">.</span><span class="n">__version__</span><span class="p">,</span> <span class="n">version_info</span><span class="o">.</span><span class="n">library_version</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="n">known_versions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_versions</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">current_version</span> <span class="ow">in</span> <span class="n">known_versions</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">+=</span> <span class="p">(</span>
                <span class="s2">&quot;Please execute `$ optuna storage upgrade --storage $STORAGE_URL` &quot;</span>
                <span class="s2">&quot;for upgrading the storage.&quot;</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">+=</span> <span class="p">(</span>
                <span class="s2">&quot;Please try updating optuna to the latest version by `$ pip install -U optuna`.&quot;</span>
            <span class="p">)</span>

        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_current_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>

        <span class="n">context</span> <span class="o">=</span> <span class="n">alembic</span><span class="o">.</span><span class="n">migration</span><span class="o">.</span><span class="n">MigrationContext</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">connect</span><span class="p">())</span>
        <span class="n">version</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_current_revision</span><span class="p">()</span>
        <span class="k">assert</span> <span class="n">version</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

        <span class="k">return</span> <span class="n">version</span>

    <span class="k">def</span> <span class="nf">get_head_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>

        <span class="n">script</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_script</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">script</span><span class="o">.</span><span class="n">get_current_head</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_get_base_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>

        <span class="n">script</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_script</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">script</span><span class="o">.</span><span class="n">get_base</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_all_versions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; List[str]</span>

        <span class="n">script</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_script</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">r</span><span class="o">.</span><span class="n">revision</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">script</span><span class="o">.</span><span class="n">walk_revisions</span><span class="p">()]</span>

    <span class="k">def</span> <span class="nf">upgrade</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">config</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_config</span><span class="p">()</span>
        <span class="n">alembic</span><span class="o">.</span><span class="n">command</span><span class="o">.</span><span class="n">upgrade</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="s2">&quot;head&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_is_alembic_supported</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="n">session</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>

        <span class="n">version_info</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">VersionInfoModel</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
        <span class="c1"># Terminate transaction explicitly to avoid connection timeout during transaction.</span>
        <span class="n">RDBStorage</span><span class="o">.</span><span class="n">_commit</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">version_info</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># `None` means this storage was created just now.</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="k">return</span> <span class="n">version_info</span><span class="o">.</span><span class="n">schema_version</span> <span class="o">==</span> <span class="n">models</span><span class="o">.</span><span class="n">SCHEMA_VERSION</span>

    <span class="k">def</span> <span class="nf">_create_alembic_script</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; alembic.script.ScriptDirectory</span>

        <span class="n">config</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_alembic_config</span><span class="p">()</span>
        <span class="n">script</span> <span class="o">=</span> <span class="n">alembic</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">ScriptDirectory</span><span class="o">.</span><span class="n">from_config</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">script</span>

    <span class="k">def</span> <span class="nf">_create_alembic_config</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; alembic.config.Config</span>

        <span class="n">alembic_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">),</span> <span class="s2">&quot;alembic&quot;</span><span class="p">)</span>

        <span class="n">config</span> <span class="o">=</span> <span class="n">alembic</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">Config</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">),</span> <span class="s2">&quot;alembic.ini&quot;</span><span class="p">))</span>
        <span class="n">config</span><span class="o">.</span><span class="n">set_main_option</span><span class="p">(</span><span class="s2">&quot;script_location&quot;</span><span class="p">,</span> <span class="n">escape_alembic_config_value</span><span class="p">(</span><span class="n">alembic_dir</span><span class="p">))</span>
        <span class="n">config</span><span class="o">.</span><span class="n">set_main_option</span><span class="p">(</span><span class="s2">&quot;sqlalchemy.url&quot;</span><span class="p">,</span> <span class="n">escape_alembic_config_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">config</span>


<span class="k">def</span> <span class="nf">escape_alembic_config_value</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="c1"># type: (str) -&gt; str</span>

    <span class="c1"># We must escape &#39;%&#39; in a value string because the character</span>
    <span class="c1"># is regarded as the trigger of variable expansion.</span>
    <span class="c1"># Please see the documentation of `configparser.BasicInterpolation` for more details.</span>
    <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;%&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">%%</span><span class="s2">&quot;</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>